require "rubygems"
require "parser"
require "amadeusdsl/connection"
require "amadeusdsl/commandparser"
require "amadeusdsl/error"



# The actual dsl
module Amadeusdsl
    extend self
    class Amadeus

        # method missing what do ?
        def method_missing(sym, *args, &block)
            puts "# unknown #{sym} with args #{args} #{block_given? ? '' : 'and code'}"
        end

        # Connection Command
        def Connection (&block)
            command = Amadeusdsl::Parser::ConnectionParser.new
            command.instance_eval(&block)
            command.execute
            command.print
            Amadeusdsl::Error::BaseError command.result
            return command.result
        end

        # Hold Command
        def Hold (&block)
            command = Amadeusdsl::Parser::HoldParser.new
            command.instance_eval(&block)
            command.execute 
            command.print
            return command.result
        end

        # Book Command
        def Book (&block)
            command = Amadeusdsl::Parser::BookParser.new
            command.instance_eval(&block)
            command.execute 
            command.print
            return command.result
        end
        
        # Cancel a booking
        def Cancel(&block)
            command = Amadeusdsl::Parser::CancelParser.new
            command.instance_eval(&block)
            command.execute
            return command.result
        end
        
        # Query the status of a booking
        def Check (&block)
            command = Amadeusdsl::Parser::QueryParser.new
            command.instance_eval(&block)
            command.execute
            command.print
            return command.result
        end


        # Check seat availibility command
        def Seats (&block)
          command = Amadeusdsl::Parser::AvailableParser.new
          command.instance_eval(&block)
          command.execute
          command.print
        end
        
        # List all destinations
        def Destinations (&s)
          command = Amadeusdsl::Parser::DestinationParser.new
          command.instance_eval(&s)
          command.execute
          command.print
          return command.result
        end


        # list all airports
        def Airports (&s)
            command = Amadeusdsl::Parser::AirportParser.new
            command.instance_eval(&s)
            command.execute
            command.print
            return command.result
        end

        #list all airlines
        def Airlines(&s)
            command = Amadeusdsl::Parser::AirlineParser.new
            command.instance_eval(&s)
            command.execute
            command.print
            return command.result
        end

        # List airports for flight
        def Route(&flight)
            command = Amadeusdsl::Parser::RouteParser.new
            command.instance_eval(&flight)
            command.execute
            command.print
        end
        
        # Return the days of the week on which a flight flies
        def Days (&flight) 
            command = Amadeusdsl::Parser::DayParser.new
            command.instance_eval(&flight)
            command.execute
            command.print
        end
        
        # Find the flights between 2 airports in a given timeperiod
        def Flights (&block)
          command = Amadeusdsl::Parser::BetweenParser.new
          command.instance_eval(&block)
          command.execute
          command.print
          return command.result
        end
        
        def Cheapest (&block)
          command = Amadeusdsl::Parser::SearchParser.new
          command.instance_eval(&block)
          commmand.SortBy("price")
          command.execute
          command.print
          return command.result
        end
        
        def Shortest (&block)
          command = Amadeusdsl::Parser::SearchParser.new
          command.instance_eval(&block)
          command.SortBy("duration")
          command.execute
          command.print
          return command.result
        end
        
        def Passenger (&block)
          command = Amadeusdsl::Parser::PassengerParser.new
          command.instance_eval(&block)
          command.execute
          command.print
          return command.result
        end
        
        def Search (&block)
          command = Amadeusdsl::Parser::SearchParser.new
          command.instance_eval(&block)
          command.execute
          command.print
          return command.result
        end
        
        def FastSearch(&block)
          command = Amadeusdsl::Parser::SecondSearchParser.new
          command.instance_eval(&block)
          command.execute
          command.print
          return command.result
        end
        
        # print whatever variable you want to see
        # also returns that was has been printed
        def Print(string)
            puts string
            return string
        end
        
        
        # Better select
        def Select(&block)
          command = Amadeusdsl::Parser::SelectParser.new
          command.instance_eval(&block)
          command.execute
          command.print
          return command.result
        end
        
        
        # confirms
        def Confirm(question = nil)
          puts question
          input = gets
          if (input.upcase[0,1] != "Y")
            return false
          end
          return true
        end
        

        # external dsl creation magic
        def self.load(filename)
            #if ! filename.end_with? ".ama"
            #    abort "Abort:: Files must have as extension .ama" 
            #end

            #new.instance_eval(Amadeusdsl::Parser.parse(File.open(filename,"r")))
            new.instance_eval(Amadeusdsl::Parser.parse(File.open(filename, "r")), filename)
        end
    end

# provide instance to test and use on
    def test
        @test ||=  Amadeus.new
    end

end


